package com.glory.good_dark.activity;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.ProjectModelListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.ProjectModelEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PMMangerActivity extends BaseProjectActivity {


    @BindView(R.id.tvEqpID)
    TextView tvEqpID;
    @BindView(R.id.tvPMType)
    TextView tvPMType;
    @BindView(R.id.tvPMState)
    TextView tvPMState;
    @BindView(R.id.ivPMTypeListArrow)
    ImageView ivPMTypeListArrow;
    @BindView(R.id.flPMTypeList)
    FrameLayout flPMTypeList;
    @BindView(R.id.rvPMListView)
    RecyclerView rvPMListView;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvPMUserGroup)
    TextView tvPMUserGroup;
    @BindView(R.id.ivPMUserGroupListArrow)
    ImageView ivPMUserGroupListArrow;
    @BindView(R.id.flPMUserGroupList)
    FrameLayout flPMUserGroupList;
    private List<String> popList;
    private List<String> userGroupList;
    private ProjectModelListAdapter projectModelListAdapter;
    private FreeUI_GeneralPop PMTypePop;
    private FreeUI_GeneralPop PMUserGroupPop;

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pm_manger;
    }

    @Override
    protected void initView() {
        tvEqpID.setText(App.EQPID);
        popList = new ArrayList<>();
        userGroupList = new ArrayList<>();
        initRecyclerView(rvPMListView,this);
        projectModelListAdapter = new ProjectModelListAdapter(null);
        projectModelListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        projectModelListAdapter.isFirstOnly(true);
        rvPMListView.setAdapter(projectModelListAdapter);
    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "\t\"data\": {\n" +
//                "\t\t\"HEADER\": {\n" +
//                "\t\t\t\"MACHINENAME\": \"PDA-MT9001\",\n" +
//                "\t\t\t\"MESSAGENAME\": \"PDAEquipmentMaintenanceInfo\",\n" +
//                "\t\t\t\"TRANSACTIONID\": \"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "\t\t\t\"EVENTTIME\": \"2019-03-16 16:28:59.585\"\n" +
//                "\t\t},\n" +
//                "\t\t\"BODY\": {\n" +
//                "\t\t\t\"EQP_ID\": \"TS00001\",\n" +
//                "\t\t\t\"OPERATOR\": \"admin\",\n" +
//                "\t\t\t\"StatusList\": [{\n" +
//                "\t\t\t\t\t\"StateName\": \"维修\"\n" +
//                "\t\t\t\t},\n" +
//                "\t\t\t\t{\n" +
//                "\t\t\t\t\t\"StateName\": \"PM保养\"\n" +
//                "\t\t\t\t}\n" +
//                "\n" +
//                "\t\t\t],\n" +
//                "\t\t\t\"INPUTDATAINFOLIST\": [{\n" +
//                "\t\t\t\t\t\"INPUTNAME\": \"保养项目1\",\n" +
//                "\t\t\t\t\t\"DATATYPE\": \"1\",\n" +
//                "\t\t\t\t\t\"DATAVALUE\": \"OK\"\n" +
//                "\t\t\t\t},\n" +
//                "\t\t\t\t{\n" +
//                "\t\t\t\t\t\"INPUTNAME\": \"保养项目1\",\n" +
//                "\t\t\t\t\t\"DATATYPE\": \"1\",\n" +
//                "\t\t\t\t\t\"DATAVALUE\": \"OK\"\n" +
//                "\t\t\t\t},\n" +
//                "\t\t\t\t{\n" +
//                "\t\t\t\t\t\"INPUTNAME\": \"保养项目1\",\n" +
//                "\t\t\t\t\t\"DATATYPE\": \"2\",\n" +
//                "\t\t\t\t\t\"DATAVALUE\": \"OK\"\n" +
//                "\t\t\t\t}\n" +
//                "\t\t\t]\n" +
//                "\t\t},\n" +
//                "\t\t\"RETURN\": {\n" +
//                "\t\t\t\"RETURNCODE\": \"0\",\n" +
//                "\t\t\t\"RETURNMESSAGE\": \"\"\n" +
//                "\t\t}\n" +
//                "\t}\n" +
//                "}";
//        BaseEntity<ProjectModelEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<ProjectModelEntity>>() {}.getType());
//        if (bean.isSuccess(getApplicationContext(),getSupportFragmentManager())) {
//            ProjectModelEntity body = bean.getData().getBody();
//            if (body.getStatusList().size() > 0){
//                for (ProjectModelEntity.Status status : body.getStatusList()){
//                    popList.add(status.getStateName());
//                }
//            }
//            projectModelListAdapter.setNewData(body.getINPUTDATAINFOLIST());
//        }
        FreeUI_EntityCallBack<BaseEntity<ProjectModelEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<ProjectModelEntity>>
                (new TypeToken<BaseEntity<ProjectModelEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<ProjectModelEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    ProjectModelEntity body = response.body().getData().getBody();
                    if (body.getStatusList().size() > 0){
                        for (ProjectModelEntity.Status status : body.getStatusList()){
                            popList.add(status.getStateName());
                        }
                        tvPMType.setText(body.getStatusList().get(0).getStateName());
                    }
                    if (body.getUserGroup().size() > 0){
                        for (ProjectModelEntity.Status status : body.getUserGroup()){
                            userGroupList.add(status.getGroupName());
                        }
                        tvPMUserGroup.setText(body.getUserGroup().get(0).getGroupName());
                    }
                    tvPMState.setText(body.getPmState());
                    projectModelListAdapter.setNewData(body.getINPUTDATAINFOLIST());
                }

            }

            @Override
            public void onError
                    (Response<BaseEntity<ProjectModelEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAEquipmentMaintenanceInfo");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EQP_ID",App.EQPID);
        map.addProperty("OPERATOR",App.userEntity.getWORKNO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",this,callBack,"PDAEquipmentMaintenanceInfo",map);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("PM管理");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tvPMType.getText().toString().equals("")){
                    showToast("请选择PM类型",false);
                }else if (emptyList(projectModelListAdapter.getData())){
                    showToast("列表不能有空值",false);
                }else if (tvPMUserGroup.getText().toString().equals("")){
                    showToast("请选择PM用户组",false);
                }else {
                    //提交
                    FreeUI_EntityCallBack<BaseEntity<ProjectModelEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<ProjectModelEntity>>
                            (new TypeToken<BaseEntity<ProjectModelEntity>>() {
                            }.getType(), getSupportFragmentManager(), this) {

                        @Override
                        public void onSuccess
                                (final Response<BaseEntity<ProjectModelEntity>> response) {
                            if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                                showConfirmDialog(PMMangerActivity.this,"提交成功");
                            }

                        }

                        @Override
                        public void onError
                                (Response<BaseEntity<ProjectModelEntity>> response) {
                            super.onError(response);
                            loadError(response.getException(), "PDAEquipmentMaintenanceSubmit");
                        }
                    };
                    JsonObject map = new JsonObject();
                    map.addProperty("EQP_ID",App.EQPID);
                    map.addProperty("OPERATOR",App.userEntity.getWORKNO());
                    map.addProperty("StateName",tvPMType.getText().toString());
                    map.addProperty("UserGroup",tvPMUserGroup.getText().toString());
                    map.addProperty("PmState",tvPMState.getText().toString());
                    map.add("INPUTDATAINFOLIST",new JsonParser().parse(new Gson().toJson(projectModelListAdapter.getData())).getAsJsonArray());
                    NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",PMMangerActivity.this,callBack,"PDAEquipmentMaintenanceSubmit",map);
                }
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private boolean emptyList(List<ProjectModelEntity.ProjectInfoEntity> data) {
        for (ProjectModelEntity.ProjectInfoEntity entity : data){
            if (entity.getDATAVALUE().equals("")){
                return true;
            }
        }
        return false;
    }


    @OnClick({R.id.flPMTypeList,R.id.flPMUserGroupList,R.id.btnStartPM})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.flPMTypeList:
                if (popList.size() <= 0){
                    showToast("未获取到下拉列表数据",false);
                    return;
                }

                if (PMTypePop != null) {
                    if (PMTypePop.isShowing()) {
                        PMTypePop.dismiss();
                    }
                } else {
                    PMTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPMTypeList.getWidth(), popList, ivPMTypeListArrow, (code, i) -> {
                        tvPMType.setText(code);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivPMTypeListArrow);
                PMTypePop.showAsDropDown(flPMTypeList, 0, 0);
                break;
            case R.id.flPMUserGroupList:
                if (userGroupList.size() <= 0){
                    showToast("未获取到下拉列表数据",false);
                    return;
                }

                if (PMUserGroupPop != null) {
                    if (PMUserGroupPop.isShowing()) {
                        PMUserGroupPop.dismiss();
                    }
                } else {
                    PMUserGroupPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPMUserGroupList.getWidth(), userGroupList, ivPMUserGroupListArrow, (code, i) -> {
                        tvPMUserGroup.setText(code);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivPMUserGroupListArrow);
                PMUserGroupPop.showAsDropDown(flPMUserGroupList, 0, 0);
                break;
            case R.id.btnStartPM:
                FreeUI_EntityCallBack<BaseEntity<ProjectModelEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<ProjectModelEntity>>
                        (new TypeToken<BaseEntity<ProjectModelEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<ProjectModelEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showToast("开始成功",true);
                            tvPMState.setText(response.body().getData().getBody().getPmState());
                        }

                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<ProjectModelEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAEquipmentMaintenanceSubmit");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("OPERATOR",App.userEntity.getWORKNO());
                map.addProperty("EQP_ID",App.EQPID);
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",this,callBack,"PDAgetDownToPM",map);
                break;
        }

    }
}